package controllers

import (
	"encoding/json"
	// "errors"
	// "strconv"
	"strings"
	"time"
	"yjk/models"

	admin "gitee.com/lzqysoft/goadmin/controllers"
	"github.com/astaxie/beego"
)

// ServController operations for Serv
type ServController struct {
	admin.CommonController
}

// URLMapping ...
func (c *ServController) URLMapping() {
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Update", c.Update)
	c.Mapping("Delete", c.Delete)
}

// GetOne ...
// @Title Get One
// @Description get Serv by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Serv
// @Failure 403 :id is empty
// @router /:id [get]
func (c *ServController) GetOne() {
	id := c.Ctx.Input.Param(":id")
	v, err := models.GetServById(id)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

type Pagedata struct {
	Pagesize  int
	Page      int
	SearchKey string
	Sortby    []string
	Order     []string
}

// GetAll ...
// @Title Get All
// @Description get Serv
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Serv
// @Failure 403
// @router /GetAll [post]
func (c *ServController) GetAll() {

	rdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(admin.ErrToken)
	}
	var pd Pagedata
	err = json.Unmarshal(rdata, &pd)
	if err != nil {
		beego.Error(err)
		c.RetError(admin.ErrInputData)
	}

	sortby := pd.Sortby
	order := pd.Order
	//	var query map[string]string = make(map[string]string)
	limit := pd.Pagesize
	offset := (pd.Page - 1) * limit
	beego.Debug(pd.SearchKey)
	searchKey := strings.TrimSpace(pd.SearchKey)
	beego.Debug(searchKey)
	l, count, err := models.GetServs(sortby, order, offset, limit, searchKey)
	logs := make(map[string]interface{})
	logs["data"] = l
	logs["total"] = count
	c.RetSuccess(logs)
}

// Update ...
// @Title Update
// @Description update the Serv
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Serv	true		"body for Serv content"
// @Success 200 {object} models.Serv
// @Failure 403 :id is not int
// @router /:id [post]
func (c *ServController) Update() {
	user := c.User
	rdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(admin.ErrToken)
	}
	var v models.Serv
	err = json.Unmarshal(rdata, &v)
	if err != nil {
		beego.Error(err)
		c.RetError(admin.ErrInputData)
	}
	u, _ := models.GetServById(v.Id)

	v.Updater = user.Name
	v.Utime = time.Now()
	err = models.UpdateServById(&v)
	if err != nil {
		beego.Error(err)
		c.RetError(admin.ErrDatabase)
	}

	c.Dlog("修改了服务实例,id为：", v.Id, 1, u)
	c.RetSuccess(v.Id)
}

// Delete ...
// @Title Delete
// @Description delete the Serv
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ServController) Delete() {
	id := c.Ctx.Input.Param(":id")
	if err := models.DeleteServ(id); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
